package com.wicket.okroperation.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wicket.okroperation.base.service.MOmsSoftwareAuthorizationService;
import com.wicket.okroperation.base.service.dto.req.*;
import com.wicket.okroperation.common.dto.FieldInfo;
import com.wicket.okroperation.common.exception.BizException;
import com.wicket.okroperation.common.tools.StringUtils;
import com.wicket.okroperation.common.util.CommonFunctionHelper;
import com.wicket.okroperation.dal.mapper.mbg.OmsSoftwareAuthorizationMapper;
import com.wicket.okroperation.dal.po.mbg.OmsSoftwareAuthorization;
import java.lang.reflect.Field;
import java.util.*;
import java.util.ArrayList;
import java.util.stream.Collectors;
//import com.wicket.okroperation.base.service.dto.res.*;

import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ：
 * @since 2022/1/19 15:39
 *
 *
 * @version 1.0
 */
@Service
public class MOmsSoftwareAuthorizationServiceImpl
  extends ServiceImpl<OmsSoftwareAuthorizationMapper, OmsSoftwareAuthorization>
  implements MOmsSoftwareAuthorizationService {

  @Autowired
  OmsSoftwareAuthorizationMapper omsSoftwareAuthorizationMapper;
/**
   * 6-1-01查询软件授权证书列表[6552]
   * gen by moon at 5/18/2023, 4:29:51 PM
   */
 @Trace(operationName = "6-1-01查询软件授权证书列表")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  List<OmsSoftwareAuthorization>  querySoftwareAuthorizationList(QuerySoftwareAuthorizationListReq reqDto) {
     
        try {
            QueryWrapper<OmsSoftwareAuthorization> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsSoftwareAuthorization.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsSoftwareAuthorization.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper,likeList,fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap,likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsSoftwareAuthorization.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            
            return ((List<OmsSoftwareAuthorization>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1",e,false);
        }
  }
/**
   * 6-1-01查询软件授权证书详情[6605]
   * gen by moon at 5/18/2023, 4:29:55 PM
   */
 @Trace(operationName = "6-1-01查询软件授权证书详情")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  OmsSoftwareAuthorization  querySoftwareAuthorizationDetail(QuerySoftwareAuthorizationDetailReq reqDto) {
     
        try {
         reqDto.setIsValid("FALSE");
            QueryWrapper<OmsSoftwareAuthorization> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsSoftwareAuthorization> retList = super.list(queryWrapper);
            if(retList!=null && retList.size()>0){
                OmsSoftwareAuthorization retData = retList.get(0);
                
                return retData;
            }
            return null;
      } catch (Exception e) {
          e.printStackTrace();
          throw new BizException("-1",e,false);
      }
  }
/**
   * 6-1-01新增软件授权证书[6607]
   * gen by moon at 5/18/2023, 4:29:55 PM
   */
 @Trace(operationName = "6-1-01新增软件授权证书")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  String  addSoftwareAuthorization(OmsSoftwareAuthorization omsSoftwareAuthorization) {
     omsSoftwareAuthorization.setAuthorizationId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
omsSoftwareAuthorization.setIsArchive("FALSE");
omsSoftwareAuthorization.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
omsSoftwareAuthorization.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
if(omsSoftwareAuthorization!=null && omsSoftwareAuthorization.getSubjectLifeCycle()!=null &&  omsSoftwareAuthorization.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsSoftwareAuthorization.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
          }
if(omsSoftwareAuthorization!=null && omsSoftwareAuthorization.getSubjectLifeCycle()!=null && omsSoftwareAuthorization.getSubjectLifeCycle().equals("ARCHIVING")) {
              omsSoftwareAuthorization.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
          }
omsSoftwareAuthorization.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
omsSoftwareAuthorization.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
if(omsSoftwareAuthorization!=null && omsSoftwareAuthorization.getSubjectLifeCycle()!=null && omsSoftwareAuthorization.getSubjectLifeCycle().equals("CURRENTLY_VALID")){
             omsSoftwareAuthorization.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
            }
if(omsSoftwareAuthorization!=null && omsSoftwareAuthorization.getSubjectLifeCycle()!=null && omsSoftwareAuthorization.getSubjectLifeCycle().equals("ARCHIVING")) {
              omsSoftwareAuthorization.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
          }
omsSoftwareAuthorization.setIsValid("FALSE");
omsSoftwareAuthorization.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
omsSoftwareAuthorization.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
     if(super.save(omsSoftwareAuthorization)){
        return omsSoftwareAuthorization.getAuthorizationId();
     }else {
        return "-1";
     }
    
  }
/**
   * 6-1-01修改软件授权证书[6612]
   * gen by moon at 5/18/2023, 4:29:56 PM
   */
 @Trace(operationName = "6-1-01修改软件授权证书")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  updateSoftwareAuthorization(OmsSoftwareAuthorization omsSoftwareAuthorization) {
     omsSoftwareAuthorization.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
if(omsSoftwareAuthorization!=null && omsSoftwareAuthorization.getSubjectLifeCycle()!=null && omsSoftwareAuthorization.getSubjectLifeCycle().equals("ARCHIVING")) {
              omsSoftwareAuthorization.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
          }
omsSoftwareAuthorization.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
if(omsSoftwareAuthorization!=null && omsSoftwareAuthorization.getSubjectLifeCycle()!=null && omsSoftwareAuthorization.getSubjectLifeCycle().equals("ARCHIVING")) {
              omsSoftwareAuthorization.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
          }
        return super.updateById(omsSoftwareAuthorization);
  }
/**
   * 6-1-01删除软件授权证书[6615]
   * gen by moon at 5/18/2023, 4:29:57 PM
   */
 @Trace(operationName = "6-1-01删除软件授权证书")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  deleteSoftwareAuthorization(String authorizationId ) {
     
      return super.removeById(authorizationId);
  }
/**
   * M6执行软件证书【循环开始】[6712]
   * gen by moon at 5/18/2023, 4:30:03 PM
   */
 @Trace(operationName = "M6执行软件证书【循环开始】")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  OmsSoftwareAuthorization  implementSoftwareAuthorizationLoopStart(ImplementSoftwareAuthorizationLoopStartReq reqDto) {
     
        try {
         
            QueryWrapper<OmsSoftwareAuthorization> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsSoftwareAuthorization> retList = super.list(queryWrapper);
            if(retList!=null && retList.size()>0){
                OmsSoftwareAuthorization retData = retList.get(0);
                
                return retData;
            }
            return null;
      } catch (Exception e) {
          e.printStackTrace();
          throw new BizException("-1",e,false);
      }
  }
/**
   * 6-1-01批量改软件授权证书[6946]
   * gen by moon at 5/25/2023, 5:06:05 PM
   */
 @Trace(operationName = "6-1-01批量改软件授权证书")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  batchUpdateSoftwareAuthorization(List<OmsSoftwareAuthorization> omsSoftwareAuthorization) {
     
      if(omsSoftwareAuthorization==null || omsSoftwareAuthorization.size()==0){
        return false;
      }
        return super.saveOrUpdateBatch(omsSoftwareAuthorization);
      
  }
}
